package com.rules.admin.utils;


import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.rules.admin.domain.SysResource;
import com.rules.admin.domain.SysResourceRelation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.Nullable;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;


public final class ResourceUtils {
    public ResourceUtils() {
    }

    public static Map<AntPathRequestMatcher, SysResource> buildResourceData(Iterable<SysResource> allResources, Iterable<SysResourceRelation> resourceRelations) {
        ImmutableMap.Builder<AntPathRequestMatcher, SysResource> resourceDataBuilder = ImmutableMap.builder();
        Iterator var3 = allResources.iterator();

        while(var3.hasNext()) {
            SysResource currentResource = (SysResource)var3.next();
            Iterable<SysResourceRelation> currentResourceRelations = filterCurrentResourceRelations(resourceRelations, currentResource);
            Iterable<SysResource> childrenByRelation = filterChildrenByRelations(currentResourceRelations, allResources);
            Iterable<SysResource> childrenByUrlPrefix = filterChildrenByUrlPrefix(currentResource, allResources);
            currentResource.setChildren(Iterables.concat(childrenByRelation, childrenByUrlPrefix));
            resourceDataBuilder.put(new AntPathRequestMatcher(currentResource.getUrl()), currentResource);
        }

        return resourceDataBuilder.build();
    }

    private static Iterable<SysResourceRelation> filterCurrentResourceRelations(Iterable<SysResourceRelation> resourceRelations, final SysResource resource) {
        return Iterables.filter(resourceRelations, new Predicate<SysResourceRelation>() {
            public boolean apply(SysResourceRelation resourceRelation) {
                return Objects.equal(resourceRelation.getParentResourceId(), resource.getResourceId());
            }
        });
    }

    private static Iterable<SysResource> filterChildrenByRelations(Iterable<SysResourceRelation> currentResourceRelations, final Iterable<SysResource> allResources) {
        return Iterables.transform(currentResourceRelations, new Function<SysResourceRelation, SysResource>() {
            @Nullable
            public SysResource apply(final SysResourceRelation resourceRelation) {
                return (SysResource)Iterables.tryFind(allResources, new Predicate<SysResource>() {
                    public boolean apply(@Nullable SysResource input) {
                        return Objects.equal(input.getResourceId(), resourceRelation.getResourceId());
                    }
                }).orNull();
            }
        });
    }

    private static Iterable<SysResource> filterChildrenByUrlPrefix(final SysResource resource, Iterable<SysResource> resources) {
        return Iterables.filter(resources, new Predicate<SysResource>() {
            public boolean apply(@Nullable SysResource input) {
                return !Objects.equal(resource.getResourceId(), input.getResourceId()) && StringUtils.startsWith(input.getUrl(), resource.getUrl());
            }
        });
    }

    public static HttpServletRequest mockRequestByRequestUrl(String requestUrl) {
        return MockHttpServletRequest.from(requestUrl);
    }
}
